using System;
using System.Collections.Generic;
using System.Text;
using SharpMud.LPC;

namespace SharpMud.Net.Intermud3.Packets
{
    /// <summary>
    /// Represents the Intermud3's "tell" packet, used to send private messages from one user to another.
    /// </summary>
    public class Tell : GenericPacket
    {
        private const string _PacketType = "tell";

        protected Tell()
            : base(new LPC.Array((LPC.String)_PacketType, (Integer)5, (LPC.String)"", (LPC.String)"", (LPC.String)"", (LPC.String)"", (LPC.String)"", (LPC.String)""))
        {
        }

        protected Tell(LPC.Array data)
            :base(data)
        {
            if (data.Count != 8)
                throw new InvalidPacketException("A 'Tell' packet requires 8 Object's, to be created.");
        }

        public Tell(string originatorMud, string originatorUsername, string targetMud, string targetUsername, string originatorVisname, string message)
            : base(new LPC.Array((LPC.String)_PacketType, (Integer)5, (LPC.String)originatorMud, (LPC.String)originatorUsername, (LPC.String)targetMud, (LPC.String)targetUsername, (LPC.String)originatorVisname, (LPC.String)message))
        {
        }

        public virtual string OriginatorVisname
        {
            get
            {
                return (string)((LPC.String)(_data[6]));
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (_data[6] is LPC.String)
                {
                    ((LPC.String)_data[6]).Value = value;
                    return;
                }
                else if (_data[6] == null)
                {
                    _data[6] = new LPC.String(value);
                    return;
                }
                throw new InvalidOperationException();
            }
        }

        public virtual string Message
        {
            get
            {
                return (string)((LPC.String)(_data[7]));
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (_data[7] is LPC.String)
                {
                    ((LPC.String)_data[7]).Value = value;
                    return;
                }
                else if (_data[7] == null)
                {
                    _data[7] = new LPC.String(value);
                    return;
                }
                throw new InvalidOperationException();
            }
        }

        public override string PacketType
        {
            get
            {
                return _PacketType;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (value.Trim().ToLower() == _PacketType)
                    return;
                else
                    throw new InvalidOperationException("A specific packet-type instance cannot be assigned PacketType's.");
            }
        }

        public static GenericPacket CreatePacket(LPC.Array data)
        {
            return new Tell(data);
        }
    }
}
